<template>
	<view>
		<view class="order">
			<view class="orderNumber borderBottom1">
				<text>订单编号</text>
				<text>{{ orderNumber }}</text>
			</view>
			<view class="orderNumber ">
				<text>金额</text>
				<text>{{ money ? '¥' + money : '' }}</text>
			</view>
		</view>
		<view class="subscribeTeacher">{{ people > 0 ? `该教练已有${people}人预约` : '暂无人预约' }}</view>
		<view class="payMethod" v-if="show">
			<view class="pay borderBottom1" @click="selectRadio = 1" v-if="yueShow">
				<view class="">
					<image style="width: 35rpx;height: 35rpx;margin-right: 28rpx;" src="../../static/img/order/wallet.png" mode=""></image>
					<text>余额支付</text>
				</view>
				<evan-radio ref="listRadio" style="margin-right: 22rpx;" v-model="selectRadio" :label="1"></evan-radio>
			</view>
			<view class="pay" @click="selectRadio = 2">
				<view class="">
					<image style="width: 42rpx;height: 36rpx;margin-right: 22rpx;" src="../../static/img/order/wechatPay.png" mode=""></image>
					<text>微信支付</text>
				</view>
				<evan-radio ref="listRadio" style="margin-right: 20rpx;" v-model="selectRadio" :label="2"></evan-radio>
			</view>
		</view>
		<view class="footer">
			<view class="" @click="submitOrder">确定支付{{ money ? '￥' + money : '' }}</view>
		</view>
		<u-mask :show="is_Show"></u-mask>
	</view>
</template>

<script>
import { weChatPay,testWeChatPay } from '@/api/user/order/order.js';
import { balancePay, payOrder,testBalancePay,testPayOrder,zeroOrder,kaoshiZeroOrder } from '@/api/user/order/subscribeOrder.js';
export default {
	components: {},
	data() {
		return {
			selectRadio: 1,
			money: '',
			orderNumber: 'QHHC20200422975149995',
			people: '',
			payFlag: true,
			type:1,
			show:true,
			is_Show:false,
			yueShow:1,
		};
	},
	onReady() {
		if (typeof window.WeixinJSBridge == 'undefined') {
			this.payFlag = false;
		} else {
			this.payFlag = true;
		}
	},
	async onLoad(options) {
		this.type=options.type;
		this.orderNumber = options.orderNumber;
		let res;
		if(this.type==1){
			res = await payOrder(options.orderNumber);
		}else{
			res = await testPayOrder(options.orderNumber);
		}
		
		if (res.code == 1) {
			this.money = res.data.money;
			if(this.money==0){
				this.show=false
			}else{
				this.yueShow=res.data.isuseyystatus
				if(this.yueShow==0) this.selectRadio=2
			}
			this.people = res.data.yuyuenumber;
			
		}

		// console.log(res);
	},
	methods: {
		async submitOrder() {
			if(this.money==0){
				let res
				if(this.type==1){
					res=await kaoshiZeroOrder(this.orderNumber);
					
				}else{
					res=await zeroOrder(this.orderNumber);
				}
				
				if (res.code == 1) {
					uni.showLoading({
					    title: '请稍候',
						mask:true
					});
					setTimeout(()=>{
						uni.hideLoading();
						uni.reLaunch({
							url: '/pages/order/paySuccess'
						});
					},1500)
					
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					});
				}
			}else{
				if (this.selectRadio == 1) {
					this.is_Show=true
					let res
					if(this.type==1){
						res = await balancePay(this.orderNumber);
					}else{
						res = await testBalancePay(this.orderNumber);
					}
					
					if (res.code == 1) {
						uni.showLoading({
						    title: '请稍候',
							mask:true
						});
						setTimeout(()=>{
							this.is_Show=false
							uni.hideLoading();
							uni.reLaunch({
								url: '/pages/order/paySuccess'
							});
						},1500)
						
					} else {
						this.is_Show=false
						uni.showToast({
							title: res.msg,
							icon: 'none'
						});
					}
				} else {
					if (this.payFlag) {
						uni.showLoading({
						    title: '请稍候',
							mask:true
						});
						let res;
						if(this.type==1){
							res = await weChatPay(this.orderNumber);
						}else{
							res = await testWeChatPay(this.orderNumber);
						}
						
						if (res.msg == 'success') {
							uni.hideLoading();
							let payInfo = JSON.parse(res.data);
							this.$wxPay(
								payInfo,
								function(res) {
									/*成功的回调*/
									if (res.err_msg == 'get_brand_wcpay_request:ok') {
										uni.reLaunch({
											url: '/pages/tabbar/personCenter/userOrder'
										});
									}
								},
								function(e) {
									/**失败的回调*/
									console.log('支付失败或取消支付');
								}
							);
						} else {
							uni.showToast({
								title: '支付失败，请退出重试',
								icon: 'none'
							});
						}
					} else {
						uni.showToast({
							title: '浏览器环境暂不支持',
							icon: 'none'
						});
					}
				}
				
			}
			
			//该方法的第一个参数payInfo为调用后台支付返回的预下单参数
			//打印payInfo
			//{
			//	"appId": "wx2*********62811f",
			//	"nonceStr": "b0c2e4491a8a5f06a1f4c0d81cd3c40c",
			//	"orderNo": 122816*********7682,
			//	"package": "prepay_id=wx14120839*********6f960fd1318413600",
			//	"sign": "F581AD6144487*********4B1CBC0AB3",
			//	"signType": "MD5",
			//	"timeStamp": 1581653318,
			//	"totalFee": 190
			//}
			//第二个参数是成功回调  第三个参数是失败回调
		}
	}
};
</script>

<style lang="scss">
@import './css/order.scss';
</style>
